package com.test.tool.demo.runner.test;

import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.test.tool.demo.runner.token.AbstractToken;
import com.test.tool.demo.runner.token.AccessTokenDetailVO;
import com.test.tool.demo.runner.token.AccessTokenVO;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 伊鸿管理平台Token
 *
 * @author 林雷
 * @date 2021年12月22日15:32:35
 **/
@Component
public class ManagerToken extends AbstractToken {

    private String testUrl = "https://test-manage.ehomepoct.com/auth/oauth/token";

    private String StageUrl = "https://sta-manage-api.ehomepoct.com/auth/oauth/token";

    private String devLoginUrl = "http://dev-manage-api.ehome.com/auth/oauth/token";

    /**
     * 登入url
     */
    private String loginUrl;

    /**
     * 登入参数
     */
    private Map<String, Object> loginParams;

    /**
     * 获取测试token
     *
     * @return
     */
    public String getTestToken(String userName,String pwd) throws Exception {
        this.loginUrl = testUrl;
        this.loginParams = this.getLoginParam("https://test-manage.ehomepoct.com/base-manage/captchaImage",userName,pwd);
        return this.getLoginToken();
    }

    /**
     * 获取预发布token
     *
     * @return
     */
    public String getStageToken(String userName,String pwd) throws Exception {
        this.loginUrl = StageUrl;
        this.loginParams = this.getLoginParam("https://sta-manage-api.ehomepoct.com/base-manage/captchaImage",userName,pwd);
        return this.getLoginToken();
    }

    /**
     * 获取DevToken信息
     *
     * @return 返回token
     */
    public String getDevToken(String userName,String pwd) throws Exception {
        this.loginUrl = devLoginUrl;
        this.loginParams = this.getLoginParam("http://dev-manage-api.ehome.com/base-manage/captchaImage",userName,pwd);
        return this.getLoginToken();
    }

    private String getLoginToken() throws Exception {
        HttpResponse authorization = HttpUtil.createPost(this.loginUrl)
            .header("authorization", getTestAuthorization())
            .form(this.loginParams)
            .execute();

        AccessTokenVO accessTokenVO = jsonToBean(authorization.body(), AccessTokenVO.class);

        // 空校验
        if (accessTokenVO == null) {
            throw new Exception("获取Token失败");
        }
        AccessTokenDetailVO data = accessTokenVO.getData();

        return data.getTokenType().concat(" ").concat(data.getAccessToken());
    }

    /**
     * 验证码前缀
     */
    private static final String VERIFICATION_CODE_PREFIX = "captcha_codes:";

    @Override
    public String getLoginUrl() {
        return "https://ismptest.ehomepoct.com//auth/oauth/token";
    }

    @Override
    public String getTestAuthorization() {
        return "Basic aW4taG91c2U6aW4taG91c2U=";
    }

    @Override
    public Map<String, Object> initLoginParam() throws Exception {
        return null;
    }

    public Map<String, Object> getLoginParam(String url, String userName, String pwd) throws Exception {
        VerificationCodeDetailVO codeDetail = initVerificationCode(url);
        HashMap<String, Object> formMap = new HashMap<>(6);
        formMap.put("username", userName);
        formMap.put("password", pwd);
        formMap.put("code", codeDetail.getVerificationCode());
        formMap.put("uuid", codeDetail.getUuid());
        formMap.put("grant_type", "password");
        formMap.put("scope", "in-house");
        return formMap;
    }

    /**
     * 获取图片验证码
     *
     * @return 返回图片验证码
     */
    private VerificationCodeDetailVO initVerificationCode(String url) throws Exception {
        // 读取图片验证码
        String response = HttpUtil.get(url);
        // 转换json
        VerificationCodeVO verificationCodeVO = jsonToBean(response, VerificationCodeVO.class);
        // 空校验
        if (verificationCodeVO == null) {
            throw new Exception("获取图片验证码失败");
        }
        // 获取Redis服务
        RedisTemplate<Object, Object> redisTemplate = SpringUtil.getBean("redisTemplate", RedisTemplate.class);
        // 读取验证码
        Object verificationCode = redisTemplate.opsForValue().get(VERIFICATION_CODE_PREFIX.concat(verificationCodeVO.getUuid()));

//        Console.log(VERIFICATION_CODE_PREFIX.concat(verificationCodeVO.getUuid()));
        // 空校验
        if (verificationCode == null) {
            throw new Exception("读取Redis验证码失败");
        }
        // 返回验证码
        VerificationCodeDetailVO data = new VerificationCodeDetailVO();
        data.setVerificationCode(verificationCode.toString());
        data.setUuid(verificationCodeVO.getUuid());
        return data;
    }

}
